package algorithms.leecode;

import edu.princeton.cs.algs4.ST;
import io.netty.util.internal.StringUtil;


import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

public class Au71 {
    public static void main(String[] args) {
//        String a = "/a/v/d/w//e";
//        String[] ww = a.split("/");
//        System.err.println(ww);
//        Deque<String> stack = new LinkedList<>();
//        stack.pollLast();
        String res = new Au71().simplifyPath("/../");
        System.err.println(res);
    }

    public String simplifyPath(String path) {
        String[] names = path.split("/../");
        Deque<String> stack = new LinkedList<String>();
        for (String name : names) {
            if ("".equals(name)) {
                continue;
            }
            if (".".equals(name)) {
                continue;
            }
            if ("..".equals(name)) {
                stack.pollFirst();
                stack.pollFirst();
                continue;
            }
            stack.addFirst("/");
            stack.addFirst(name);
        }
        StringBuilder builder = new StringBuilder();
        while (!stack.isEmpty()) {
            builder.append(stack.pollLast());
        }
        if (stack.isEmpty()) {
            builder.append("/");

        }

      return builder.toString();
    }
}
